<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/24
 * Time: 15:26
 * QQ:1467572213
 */

namespace app\admin\model;

use think\facade\Request;
use think\Model;
use think\Db;
use think\facade\Config;
use think\facade\Session;

class Withdrawal extends Model
{
    public static function index($keyword,$order_id,$create_time,$status)
    {
        date_default_timezone_set('America/Mexico_City');
        $map = [];
        // $map[]=['agent_check','=',1];
        if ($keyword) {
            $map[] = ['username', 'like', '%'.trim($keyword).'%'];
        } 
        if ($order_id) {
            $map[] = ['order_id', '=', $order_id];
        }
        if ($create_time) {
            $create_time1 =$create_time.' 00:00:00';
            $create_time2 =$create_time.' 23:59:59';
            $map[] = ['create_time', '>', strtotime($create_time1)];
            $map[] = ['create_time', '<', strtotime($create_time2)];
        }
        if ($status) {
            $map[] = ['status', '=', $status];
        }
        $list = Db::name('fd_withdrawal')
        ->where($map)
        ->order('id', 'desc')
        ->paginate(50, false, ['query' => request()->param()]);
        return $list;
        // else {
        //     $list = Db::name('fd_withdrawal')->order('id', 'desc')
        //         ->paginate(50, false, ['query' => request()->param()]);
        //     return $list;
        // }
    }

    public static function get_withdrawal_info($id)
    {
        $info = Db::name('fd_withdrawal')->where('id',$id)->find();
        return $info;
    }

    public static function withdrawal_pass($id)
    {
        $info = Db::name('fd_withdrawal')->where('id',$id)->find();
        $oceanpay_w_url = Config::get('oceanpay_w_url');

        $sign_data['code'] = Config::get('oceanpay_w_code');
        $sign_data['amount'] = $info['actual_money'];
        $sign_data['bankname'] = $info['bank_name'];
        $sign_data['accountname'] = $info['holder_name'];
        $sign_data['cardnumber'] = $info['account_number'];
        $sign_data['mobile'] = $info['phone_number'];
        $sign_data['email'] = $info['email'];
        $sign_data['starttime'] = $info['create_time'];
        $sign_data['notifyurl'] = Request::domain().'/index/notify/oceanpay_w';
        $sign_data['ifsc'] = $info['ifsc'];
        $sign_data['merissuingcode'] = $info['order_id'];
        $sign_data['key'] = Config::get('oceanpay_w_key');
        $sign = self::oceanpay_w_sign($sign_data);

        $post['code'] = Config::get('oceanpay_w_code');
        $post['amount'] = $info['actual_money'];
        $post['bankname'] = $info['bank_name'];
        $post['accountname'] = $info['holder_name'];
        $post['cardnumber'] = $info['account_number'];
        $post['mobile'] = $info['phone_number'];
        $post['email'] = $info['email'];
        $post['starttime'] = $info['create_time'];
        $post['notifyurl'] = Request::domain().'/index/notify/oceanpay_w';
        $post['ifsc'] = $info['ifsc'];
        $post['merissuingcode'] = $info['order_id'];
        $post['signs'] = $sign;

        $res = self::oceanpay_w_post($oceanpay_w_url,$post);
        // var_dump($res);die;
        if ($res['msg'] == 'success'){
            Db::name('fd_withdrawal')->where('id',$id)->setField('status',3);
            return 'success';
        }else{
            return $res['msg'];
        }

    }

    public static function oceanpay_w_sign($sign_data)
    {
        $hmacstr = 'accountname='.$sign_data['accountname'].'&amount='.$sign_data['amount'].'&bankname='.$sign_data['bankname'].'&cardnumber='.$sign_data['cardnumber'].'&code='.$sign_data['code'].'&email='.$sign_data['email'].'&ifsc='.$sign_data['ifsc'].'&merissuingcode='.$sign_data['merissuingcode'].'&mobile='.$sign_data['mobile'].'&notifyurl='.$sign_data['notifyurl'].'&starttime='.$sign_data['starttime'].'&key='.$sign_data['key'].'';

        $sign= strtoupper(md5($hmacstr));
        return $sign;
    }

    public static function oceanpay_w_post($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);
        curl_close($ch);
        $rst = json_decode($output,true);
        return $rst;
    }

    public static function withdrawal_refuse($id)
    {
        $res = Db::name('fd_withdrawal')->where('id',$id)->setField('status',2);
        if ($res == true){
            $withdrawal_info = self::get_withdrawal_info($id);
            $refund = Db::name('fd_user')
                ->where('id',$withdrawal_info['uid'])->setInc('balance',$withdrawal_info['apply_money']);
            if ($refund == true){
                return 'success';
            }else{
                return 'error';
            }
        }else{
            return 'error_status';
        }
    }

}